package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ContractInfo;
import com.sl.au.entity.Module;
import com.sl.au.entity.Option;
import com.sl.au.service.ContractQueryService;

/**
 * 合同全流程综合查询控制器
 * @author XH
 *
 */
@RestController
@RequestMapping(value = "/ui/contractQuery/")
public class ContractQueryController extends BaseController {

	
	@Autowired
	ContractQueryService contractQueryService;
	
	/**
	 * 合同全流程综合查询页面显示
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("contractquery/contractqueryindex");
		
		modelAndView.addObject("outDateLists", contractQueryService.getOutDateLists());
		return modelAndView;
	}
	
	/**
	 * 获取页面数据
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		String name="contractsearch"; 
        Module module = this.getPageModule(name);
		String userId = a.getUser().getId();
		String superior = a.getUser().getSaleArea().getSuperior(); 
		String areaName=a.getUser().getSaleArea().getAreaName();
        String dataFilter=module.getDataFilter();
		String supplierCode = request.getParameter("supplierCode");
		String contractType = request.getParameter("contractType");
		String minSumStr = request.getParameter("minSum");
		String maxSumStr = request.getParameter("maxSum");
		String startDateStr = request.getParameter("startDate");
		String endDateStr = request.getParameter("endDate");
		String startDate1Str = request.getParameter("startDate1");
		String endDate1Str = request.getParameter("endDate1");
		String outDateStr = request.getParameter("outDate");
		String orderUnit = request.getParameter("orderUnit");
		condition.put("userId", userId);
		condition.put("superior", superior);
		condition.put("areaName", areaName);
		condition.put("dataFilter", dataFilter);
		condition.put("supplierCode", supplierCode);
		condition.put("contractType", contractType);
		condition.put("minSumStr", minSumStr);
		condition.put("maxSumStr", maxSumStr);
		condition.put("startDateStr", startDateStr);
		condition.put("endDateStr", endDateStr);
		condition.put("startDate1Str", startDate1Str);
		condition.put("endDate1Str", endDate1Str);
		condition.put("outDateStr", outDateStr);
		condition.put("orderUnit", orderUnit);
		List<ContractInfo> contracts = contractQueryService.getContractInfos(start, length, condition, null, null);
		long count = contractQueryService.getContractInfosCount(condition);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}


}
